package com.Wzs.lbtcp;

import java.sql.SQLException;
import java.util.ArrayList;
import com.wzsck.util.DBManager;
import com.wzsck.util.QueryDataResult;

public class LBTManager {

	//查询query
		public ArrayList<lbtcp> query(){//把东西存到动态数组里面
			ArrayList<lbtcp> list = new ArrayList<lbtcp>();
			String sql = "select * from wzs_broadcastmapproduct";//sql语句查表book里的东西
			QueryDataResult qdr =DBManager.executeQuery(sql);
			if (qdr.rs!=null){
				try {
					while (qdr.rs.next()){
						int id=qdr.rs.getInt("id");//编号,后面是获取book实体类里的id getInt（“id”）
						String name=qdr.rs.getString("name");//书名
						String imgurl=qdr.rs.getString("imgurl");//图片
						String describe=qdr.rs.getString("describe");//介绍
						int price=qdr.rs.getInt("price");//价格
						lbtcp x= new lbtcp(id,name,imgurl,describe,price);
						list.add(x);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return list;
		}
		//add
		public int add( String name,String price, String describe,String imgurl) {
			String sql = String.format(
					"insert into wzs_broadcastmapproduct(`name`,`imgurl`,`describe`,`price`) values('%s','%s','%s',%s)",name, imgurl, describe,  price);
			
			System.out.print(sql);
			int i = DBManager.executeUpdate(sql);
			return i;

		}
		
	
		public int update(String id,  String name,String price, String describe,String imgurl) {
			System.out.print("更新");
			String sql = String.format(
					"  update wzs_broadcastmapproduct set `name`='%s',`price`='%s',`describe`='%s' ,`imgurl` ='%s' where id=%s  ",
					 name,  price, describe, imgurl, id);
			System.out.print(sql);
			
			int i = DBManager.executeUpdate(sql);
			return i;
		}
		public int del(int id) {
			String sql = "delete from wzs_broadcastmapproduct where id =  " + id;
			int i = DBManager.executeUpdate(sql);
			return i;
		}
		
		//==========================================================
		 public int count() {//count查非空
		 		String sql="select count(*) from wzs_broadcastmapproduct";//换表名
		 		QueryDataResult qdr=DBManager.executeQuery(sql);
		 		if (qdr.rs!=null) {
		 			try {
		 				while (qdr.rs.next()) {
		 					int count =qdr.rs.getInt("count(*)");
		 					return count;
		 				}
		 			} catch (SQLException e) {
		 				// TODO Auto-generated catch block
		 				e.printStackTrace();
		 			}
		 		}
		 		return 0;
		 	}
		 
		     public ArrayList<lbtcp> chaxun(int page,int limit) {
		 	    ArrayList<lbtcp> list =new ArrayList<lbtcp>();//limit返回行数 limit 开始数 , 行数
					String sql="select * from  wzs_broadcastmapproduct  limit "+(page - 1)*limit+","+limit;//换表明
					QueryDataResult qdr=DBManager.executeQuery(sql);
					lbtcp x=null;
					if (qdr.rs!=null){
						try {
							while (qdr.rs.next()){
								int id=qdr.rs.getInt("id");//编号,后面是获取book实体类里的id getInt（“id”）
								String name=qdr.rs.getString("name");//书名
								String imgurl=qdr.rs.getString("imgurl");//图片
								String describe=qdr.rs.getString("describe");//介绍
								int price=qdr.rs.getInt("price");//价格
								x= new lbtcp(id,name,imgurl,describe,price);
								list.add(x);
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					qdr.close();
					return list;		
			} 
}
